package ms.shop.service;

import com.github.pagehelper.PageHelper;
import ms.common.exception.BizException;
import ms.common.page.Page;
import ms.common.util.bean.BeanUtil;
import ms.common.util.file.FileClient;
import ms.common.util.string.StringUtil;
import ms.shop.bean.BannerParamBean;
import ms.shop.bean.BannerQueryParamBean;
import ms.shop.config.Constant;
import ms.shop.config.ResponseCode;
import ms.shop.dao.BannerDAO;
import ms.shop.entity.Banner;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

/**
 * Project Name: ms
 * File Name: BannerService
 * Package Name: ms.shop.service
 * Date: 2018/5/30  16:39
 * Copyright (c) 2018, tianyul All Rights Reserved.
 */
@Service
public class BannerService {

    @Autowired
    private BannerDAO bannerDAO;

    /**
     * 分页查询
     * @param bannerQueryParamBean
     * @param key
     * @return
     */
    // 因为配置文件继承了CachingConfigurerSupport，所以没有指定key的话就是用默认KEY生成策略生成,我们这里指定了KEY
    // value属性指定Cache名称
    // 使用key属性自定义key
    // condition属性指定发生的条件(如上示例表示只有当user的id为偶数时才会进行缓存, condition = "#id%2==0")
    @Cacheable(value = "queryBannerPage", key = "'queryBannerPage' + #bannerQueryParamBean.pageNo + '-' +#bannerQueryParamBean.getType()+ '-' + #bannerQueryParamBean.getUrl()", condition="#key==''" )
    public Page<Banner> query(BannerQueryParamBean bannerQueryParamBean, String key) {
        //使用pageHelper插件进行分页查询
        com.github.pagehelper.Page page = PageHelper.startPage(bannerQueryParamBean.getPageNo(), bannerQueryParamBean.getPageSize());
        List<Banner> banners = bannerDAO.queryBanners(bannerQueryParamBean.getName(), bannerQueryParamBean.getType(), bannerQueryParamBean.getUrl());

        return new Page(page.getPageNum(), page.getPageSize(), (int)page.getTotal(), banners);
    }

    /**
     * 新增
     * @param bannerParamBean
     */
    @CacheEvict(value= {"queryBannerPage"},allEntries=true)// 清空 user_detail queryPage 缓存
    @Transactional(rollbackFor = Exception.class, isolation = Isolation.READ_COMMITTED)
    public void create(BannerParamBean bannerParamBean) {
        //新增
        Banner banner = BeanUtil.copy(bannerParamBean, Banner.class);
        banner.setStatus(2);
        banner.setCreateTime(System.currentTimeMillis());
        banner.setUpdateTime(System.currentTimeMillis());

        bannerDAO.insert(banner);
    }

    /**
     * 修改
     * @param bannerParamBean
     */
    @Caching(put = @CachePut(value = "banner_detail", key = "'banner_detail' + #bannerParamBean.id"), evict = { @CacheEvict(value = "queryBannerPage", allEntries = true)})
    @Transactional(rollbackFor = Exception.class, isolation = Isolation.READ_COMMITTED)
    public void update(BannerParamBean bannerParamBean) {
        Banner currentBanner = bannerDAO.selectByPrimaryKey(bannerParamBean.getId());
        if(StringUtil.isEmpty(currentBanner)) {
            throw new BizException(ResponseCode.BANNER_UN_EXIST, "banner不存在");
        }
        //修改
        Banner banner = BeanUtil.copy(bannerParamBean, Banner.class);
        if(StringUtil.isEmpty(banner.getImg())) {
            banner.setImg(currentBanner.getImg());
        }
        if(StringUtil.isEmpty(banner.getDetailImg())) {
            banner.setDetailImg(currentBanner.getDetailImg());
        }
        banner.setStatus(null);
        banner.setCreateTime(null);
        banner.setUpdateTime(System.currentTimeMillis());

        bannerDAO.updateByPrimaryKeySelective(banner);
    }

    /**
     * 启用
     * @param id
     */
    @Caching(put = @CachePut(value = "banner_detail", key = "'banner_detail' + #id"), evict = { @CacheEvict(value = "queryBannerPage", allEntries = true)})
    @Transactional(rollbackFor = Exception.class, isolation = Isolation.READ_COMMITTED)
    public void use(Long id) {
        Banner banner = bannerDAO.selectByPrimaryKey(id);
        if(StringUtil.isEmpty(banner)) {
            throw new BizException(ResponseCode.BANNER_UN_EXIST, "banner不存在");
        }
        if(1 == banner.getStatus()) {
            throw new BizException(ResponseCode.BANNER_STATUS_ERROR, "banner状态错误");
        }
        banner.setStatus(1);
        bannerDAO.updateByPrimaryKeySelective(banner);
    }

    /**
     * 停用
     * @param id
     */
    @Caching(put = @CachePut(value = "banner_detail", key = "'banner_detail' + #id"), evict = { @CacheEvict(value = "queryBannerPage", allEntries = true)})
    @Transactional(rollbackFor = Exception.class, isolation = Isolation.READ_COMMITTED)
    public void cancel(Long id) {
        Banner banner = bannerDAO.selectByPrimaryKey(id);
        if(StringUtil.isEmpty(banner)) {
            throw new BizException(ResponseCode.BANNER_UN_EXIST, "banner不存在");
        }
        if(2 == banner.getStatus()) {
            throw new BizException(ResponseCode.BANNER_STATUS_ERROR, "banner状态错误");
        }
        banner.setStatus(2);
        bannerDAO.updateByPrimaryKeySelective(banner);
    }

    /**
     * 详情
     * @param id
     */
    @Cacheable(value = "banner_detail", key = "'banner_detail' + #id")
    public Banner detail(Long id) {
        Banner banner = bannerDAO.selectByPrimaryKey(id);
        if(StringUtil.isEmpty(banner)) {
            throw new BizException(ResponseCode.BANNER_UN_EXIST, "banner不存在");
        }
        return banner;
    }

    @CacheEvict(value = {"queryBannerPage", "banner_detail"}, allEntries = true)
    public void clear() {
    }
}
